/*
 * $Id: SFCodeTemplate31.xml 67218 2009-03-18 19:07:17Z svn $
 */
package org.crudeq.kernel;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import com.siteseat.common.foundation.Rater;
import com.siteseat.common.misc.SizeUnit;

/**
 *
 * @author yqiang
 * Success Factors
 */
public class CachedWrite {

  public static void main(String[] args) throws Exception {
    File f = new File("c:\\cached.txt");
    if( f.exists() ) {
      f.delete();
    }
    
    int bufCapacity = 1024 * 1024 * 1;
    byte[] buf = new byte[bufCapacity];
    
    byte[] msg = new byte[256];
    
    Rater rater = new Rater( SizeUnit.MB, TimeUnit.SECONDS );
    
    FileOutputStream fos = new FileOutputStream( f );
    FileDescriptor fd = fos.getFD();
    try {  
      rater.start();
      
      for( int i = 0; i < 16 * 16; i++ ) {
        int offset = 0;
        while( offset + msg.length < buf.length ) {
          System.arraycopy( msg, 0, buf, offset, msg.length );
          offset += msg.length;
          rater.increase( msg.length );
        }
        fos.write( buf );
        fd.sync();
      }
    }finally{
      fd.sync();
      fos.close();
      
      rater.stop();
      System.out.println(rater.reportAll(""));
    }
  }
}
